﻿using IntelligentAgriculture.Contracts.Dto.Input;
using IntelligentAgriculture.Contracts.Dto.Output;
using IntelligentAgriculture.Contracts.Interface.SheepFileModelInterface;
using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.WebAPI.Application.Command.LambweaninginfoCommand;
using IntelligentAgriculture.WebAPI.Application.Command.SheepfilemodelCommand;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace IntelligentAgriculture.WebAPI.Controllers
{
    /// <summary>
    /// 羊只档案
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [AllowAnonymous]
    public class SheepFileModelsController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ISheepFileModelService _sheepFileModelService;
        private readonly ILogger<SheepFileModelsController> _logger;
        public  SheepFileModelsController(IMediator mediator, ISheepFileModelService sheepFileModelService,ILogger<SheepFileModelsController> logger)
        {
            _mediator = mediator;
            _sheepFileModelService = sheepFileModelService;
            _logger = logger;
        }
        /// <summary>
        /// 羊只档案列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        
        public async Task<Results<PageOutput<SheepfilemodelsOutput>>> GetSheepfilemodel( SheepfilemodelInput? input)
        {
            try
            {
                _logger.LogInformation("这是羊只档案列表功能");
                var command = new SheepfilemodelsGetCommand(input);
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError("羊只档案列表功能异常信息:" + ex.Message);
                throw;
            }
            
        }
        /// <summary>
        /// 档案添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Results<int>> AddSheepfilemodel(SheepfilemodelsAddCommand command)
        {
            try
            {
                _logger.LogInformation("这是羊只档案添加功能");
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError("羊只档案添加功能异常信息:" + ex.Message);
                throw;
            }
            
        }
        /// <summary>
        /// 档案删除
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Results<int>> DeleteSheepfilemodel(SheepfilemodelsDeleteCommand command)
        {
            try
            {
                _logger.LogInformation("这是羊只档案删除功能");
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError("羊只档案删除功能异常信息:" + ex.Message);
                throw;
            }
            
        }
        /// <summary>
        /// 档案反填
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Results<SheepfileInfo>> ListSheepfilemodel(int sheepFileId)
        {
            try
            {
                _logger.LogInformation("这是羊只档案反填功能");
                var command = new SheepfileListCommand(sheepFileId);
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError("羊只档案反填功能异常信息:" + ex.Message);
                throw;
            }
            
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Results<int>> UpdaSheepfilemodel(UpdaSheepfileModelInput input)
        {
            var command = new SheepfilemodelUpdateCommand(input);
            return await _mediator.Send(command);

        }
        /// <summary>
        /// 获取父和母的父母辈
        /// </summary>
        /// <param name="Queryable"></param>
        /// <param name="Ear"></param>
        /// <param name="level"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<string>> GetEarTag(IQueryable<Sheepfilemodel> Queryable, string eartag, int level)
        {
            var result =await _sheepFileModelService.GetEarTag(Queryable, eartag, level);
            return result;
        }
        /// <summary>
        /// 获取3代关系表
        /// </summary>
        /// <param name="sId">主键</param>
        /// <returns>返回三代关系表</returns>
        [HttpGet]
        public async Task<List<string>> GetGenealogy(int sheepfileId)
        {
            var list =await _sheepFileModelService.GetGenealogy(sheepfileId);
            return list;
        }
		/// <summary>
		/// 获取养只档案状态
		/// </summary>
		/// <param name="sId">主键</param>
		/// <returns>返回三代关系表</returns>
		[HttpGet]
		public async Task<Results<IQueryable<Sheepfilemodel>>> IsSexSheepfilemodel(int presentState, int sheepType)
		{

			var command =new IsSexSheepfilemodelCommand(presentState, sheepType);
			return await _mediator.Send(command);
		}
		/// <summary>
		/// 根据耳号反填羊只档案
		/// </summary>
		/// <returns></returns>
		[HttpGet]
		public async Task<Results<Sheepfilemodel>> DteilSheepfilemodel(string eartag)
		{
			try
			{
				var command = new DteilSheepfilemodelCommand(eartag);
				return await _mediator.Send(command);
			}
			catch (Exception ex)
			{
				_logger.LogError("反填羔羊断奶出错" + ex.Message);
				throw;
			}
		}
        /// <summary>
        /// 根据生长阶段和在场状态获取耳号
        /// </summary>
        [HttpGet]
		public async Task<Results<IQueryable<Sheepfilemodel>>> GrothSheepfilemodel([FromQuery]GrothSheepfilemodelCommand command)
        {
            try
            {
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError("根据生长阶段和在场状态获取耳号出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 根据繁殖状态和在场状态获取耳号
        /// </summary>
        [HttpGet]
        public async Task<Results<IQueryable<Sheepfilemodel>>> GetReproductionState([FromQuery] GetReproductionStateCommand command)
        {
            try
            {
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError("根据生长阶段和在场状态获取耳号出错" + ex.Message);
                throw;
            }
        }








    }
}
